#coding:utf-8
from bmob import *
import json
import pdb

class FixDB:
        def __init__(self):
                self.b = Bmob("f5b3dbd027720a6f49c87b6a1a1c7a84", "616ee9149ae817bcd2f535b9a21050d4")
                self.lst_dynamic = []
                self.lst_user = []
                self.lst_plan = []

        def getDynamics(self):
                ret_str = self.b.find(
                        't_dynamic'
                ).stringData
                lst_ret = json.loads(ret_str)['results']
                self.lst_dynamic = lst_ret
                #pdb.set_trace()
                return self.lst_dynamic

        def changeImageUrl(self):
                new_lst_dynamic = self.lst_dynamic
                index = 0
                for item in self.lst_dynamic:
                        new_data_list = []
                        data_list = item["data_list"]
                        for it in data_list:
                                new_url = it.replace("dailyupward.xayoushun.com","media.dailyupward.com")
                                new_data_list.append(new_url)
                        new_lst_dynamic[index]["data_list"] = new_data_list
                        index += 1
                self.lst_dynamic = new_lst_dynamic
                #pdb.set_trace()
                return self.lst_dynamic
                        
        
        def updateDynamics(self):
                #pdb.set_trace()
                index = 0
                for item in self.lst_dynamic:
                        if('data_content' in item.keys()):
                                print(str(index) + ":" + item["data_content"])
                        else:
                                print(str(index) + ":")
                        #pdb.set_trace()
                        if('data_list' in item.keys()):
                                self.b.update(
                                        't_dynamic',
                                        item['objectId'],
                                        BmobUpdater.increment("count",0,{
                                                "data_list" : item["data_list"]
                                        })
                                )
                        index += 1

        def getUsers(self):
                ret_str = self.b.find(
                        "_User"
                ).stringData
                lst_ret = json.loads(ret_str)['results']
                self.lst_user = lst_ret
                #pdb.set_trace()
                return self.lst_user

        def changeUserImageUrl(self):
                new_lst_user = self.lst_user
                index = 0
                #pdb.set_trace()
                for item in self.lst_user:
                        if('avatarUrl' in item.keys()):
                                avatar_url  = item["avatarUrl"]
                                avatar_url = avatar_url.replace("dailyupward.xayoushun.com","media.dailyupward.com")
                                new_lst_user[index]["avatarUrl"] = avatar_url
                        index += 1
                self.lst_user = new_lst_user
                #pdb.set_trace()
                return self.lst_user

        def updateUsers(self):
                #pdb.set_trace()
                index = 0
                for item in self.lst_user:
                        if('nickName' in item.keys()):
                                print(str(index) + ":" + item["nickName"])
                        else:
                                print(str(index) + ":")
                        #pdb.set_trace()
                        if('avatarUrl' in item.keys()):
                                str_result = self.b.update(
                                        "_User",
                                        item['objectId'],
                                        BmobUpdater.increment("count",0,{
                                                "avatarUrl" : item["avatarUrl"]
                                        })
                                ).jsonData
                                print(str_result)
                        index += 1

        def getPlans(self):
                ret_str = self.b.find(
                        "t_plan"
                ).stringData
                lst_ret = json.loads(ret_str)['results']
                self.lst_plan = lst_ret
                #pdb.set_trace()
                return self.lst_plan

        def changePlanImageUrl(self):
                new_lst_plan = self.lst_plan
                index = 0
                for item in self.lst_plan:
                        plan_pic  = item["plan_pic"]
                        plan_pic = plan_pic.replace("dailyupward.xayoushun.com","media.dailyupward.com")
                        new_lst_plan[index]["plan_pic"] = plan_pic
                        index += 1
                self.lst_user = new_lst_plan
                #pdb.set_trace()
                return self.lst_plan

        def updatePlans(self):
                #pdb.set_trace()
                index = 0
                for item in self.lst_plan:
                        if('plan_name' in item.keys()):
                                print(str(index) + ":" + item["plan_name"])
                        else:
                                print(str(index) + ":")
                        #pdb.set_trace()
                        if('plan_pic' in item.keys()):
                                self.b.update(
                                        "t_plan",
                                        item['objectId'],
                                        BmobUpdater.increment("count",0,{
                                                "plan_pic" : item["plan_pic"]
                                        })
                                )
                        index += 1

if __name__ == "__main__":
        fix_db = FixDB()
        #fix_db.getDynamics()
        #fix_db.changeImageUrl()
        #fix_db.updateDynamics()
        print("********更新t_dynamic表完毕**********")
        #fix_db.getUsers()
        #fix_db.changeUserImageUrl()
        #fix_db.updateUsers()
        print("********更新_User表完毕**********")
        fix_db.getPlans()
        fix_db.changePlanImageUrl()
        fix_db.updatePlans()
